package allinpay

import (
    "fmt"
	//"errors"
	//"strings"
	"encoding/xml"
	"thirdpaysdk/utils/mapstruct"
	//"github.com/astaxie/beego"
)

type AllinpayMsg struct {
	Appid       string `form:"appid"`      //
	Outtrxid    string `form:"outtrxid"`   //
	Trxcode     string `form:"trxcode"`
	Trxid       string `form:"trxid"`
	Trxamt      string `form:"trxamt"`
	Trxdate     string `form:"trxdate"`
	Paytime     string `form:"paytime"`
	Chnltrxid   string `form:"chnltrxid"`
	Trxstatus   string `form:"trxstatus"`
	Cusid       string `form:"cusid"`
	Termno      string `form:"termno"`
	Termbatchid string `form:"termbatchid"`
	Termtraceno string `form:"termtraceno"`
	Termauthno  string `form:"termauthno"`
	Termrefnum  string `form:"termrefnum"`
	Trxreserved string `form:"trxreserved"`
	Srctrxid    string `form:"srctrxid"`
	Cusorderid  string `form:"cusorderid"`
	Acct        string `form:"acct"`
	Fee         string `form:"fee"`
	Signtype    string `form:"signtype"`
	Cmid        string `form:"cmid"`
	Chnlid      string `form:"chnlid"`
	Sign        string `form:"sign"`
}

func AllinpayMsgNotify(body []byte) (map[string]string, error){
	var response AllinpayMsg
	err := xml.Unmarshal(body, &response)
	if err != nil {
		return map[string]string{}, err
	}
	rspMap :=  mapstruct.Struct2Map(response, "xml")
	reqMap := make(map[string]string)
	for k, v := range rspMap {
		reqMap[k] = fmt.Sprintf("%v", v)
	}
	return reqMap, nil
}

func AllinpayFormNotify(response AllinpayMsg) (map[string]string, error){
	rspMap :=  mapstruct.Struct2Map(response, "form")
	reqMap := make(map[string]string)
	for k, v := range rspMap {
		reqMap[k] = fmt.Sprintf("%v", v)
	}
	return reqMap, nil
}
